package cn.js.sip.rat.task.quartz;


import cn.js.sip.rat.task.entity.JobInfoEntity;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName: cn.js.sip.rat.task.quartz
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2020-01-13   20:22
 */
public class QuartzJobExecution implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobInfoEntity jonEntity = (JobInfoEntity)context.getMergedJobDataMap().get("entity");
        String beanName = jonEntity.getBeanClass();
        try {
            Class<?> aClass = Class.forName(beanName);
            Object o = aClass.newInstance();
            if(jonEntity.getParamValue()==null){
                Method declaredMethod = aClass.getDeclaredMethod(jonEntity.getMethodName());
                declaredMethod.invoke(o);
            }else{
                String[] split = jonEntity.getParamValue().split(",");
                if(split.length==1){
                    Method declaredMethod = aClass.getDeclaredMethod(jonEntity.getMethodName(),String.class);
                    declaredMethod.invoke(o,jonEntity.getParamValue());
                }


            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
